﻿using System.Diagnostics;
using WAU.DAL;
using WAU.Models.BenchmarkModels;

namespace WAU.Services
{
    public class BenchmarkService
    {
        private readonly EssentialDbContext dbContext;

        public BenchmarkService(EssentialDbContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public long InsertGuids(IEnumerable<GuidTest> guidTests)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            dbContext.Set<GuidTest>().AddRange(guidTests);
            dbContext.SaveChanges();
            stopwatch.Stop();

            return stopwatch.ElapsedMilliseconds;
        }

        /// <summary>
        /// 获取数据库连接并等待相当长的时间（测试连接池耗尽问题）
        /// </summary>
        public void GetDbConnectionAndHold()
        {
            var ddd = this.dbContext.Set<GuidTest>().FirstOrDefault();
            Console.WriteLine($"dbContext.ContextId.InstanceId is { dbContext.ContextId.InstanceId }, Lease is { dbContext.ContextId.Lease }");
            Thread.Sleep(10 * 1000);
        }
    }
}
